using System.Linq;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.GamePlay;

namespace IQIGame.Onigao.Logic
{
    public class SkillTrigger_SkillDamageTempModify : SkillTrigger
    {
        protected override ESkillTriggerType triggerType => ESkillTriggerType.SkillDamageTempModify;

        private ETargetRelation attackerRealtion;
        private ETargetRelation defenderRealtion;
        private ESkillType skillType;
        private EDamageType damageType;
        private int skillTag;

        protected override void OnInitialize(Skill rSkill, TriggerConfig rConfig)
        {
            base.OnInitialize(rSkill, rConfig);
            if (rConfig.customConfig is SkillTrigger_SkillDamageTempModify_ConfigData rConfigData)
            {
                this.attackerRealtion = (ETargetRelation)rConfigData.attackerRealtion;
                this.defenderRealtion = (ETargetRelation)rConfigData.defenderRealtion;
                this.skillType = (ESkillType)rConfigData.skillType;
                this.damageType = (EDamageType)rConfigData.damageType;
                this.skillTag = rConfigData.skillTag;
            }
        }

        protected override void OnEnable()
        {
            base.OnEnable();
            this.AddListener<SkillEffectiveContext>(EBattleTriggerType.Battle_SkillDamageTempModify, this.OnEvent);
        }

        private void OnEvent(SkillEffectiveContext rContext)
        {
            // 自己的技能执行事件不允许触发自己
            if (rContext.part.guid == this.triggerPart)
            {
                return;
            }

            if (this.attackerRealtion != ETargetRelation.None && rContext.correct.attacker != null)
            {
                var rAtkRealtion = BattleUtil.CheckRelation(this.executer, rContext.correct.attacker);
                if ((rAtkRealtion & this.attackerRealtion) == 0)
                {
                    return;
                }
            }
            if (this.defenderRealtion != ETargetRelation.None && rContext.correct.defender != null)
            {
                var rDefRealtion = BattleUtil.CheckRelation(this.executer, rContext.correct.defender);
                if ((rDefRealtion & this.defenderRealtion) == 0)
                {
                    return;
                }
            }

            var rTriggerSkill = rContext.skill;
            if (this.skillType != ESkillType.None && (rTriggerSkill.skillType & this.skillType) == 0)
            {
                return;
            }
            if (this.damageType != EDamageType.None && ((EDamageType)(1 << rTriggerSkill.config.DamageType) & this.damageType) == 0)
            {
                return;
            }

            if (this.skillTag != 0 && !rTriggerSkill.config.SkillTag.Contains(this.skillTag))
            {
                return;
            }

            var rVirInput = ClassPool.Get<SelectParam>();
            rVirInput.unit = this.skill.executer;
            rVirInput.pos = rVirInput.unit.pos;
            this.OnTrigger(rVirInput, rContext.correct);
        }

        public override void OnReset()
        {
            base.OnReset();
            this.attackerRealtion = 0;
            this.defenderRealtion = 0;
        }

        public override void Free()
        {
            ClassPool.Put(this);
        }

#if UNITY_EDITOR
        public override string ToString()
        {
            return $"伤害前触发修正";
        }
#endif
    }
}